=begin pod

=TITLE class BagHash

=SUBTITLE Mutable collection of distinct objects with integer weights

    class BagHash does Baggy { }

A C<BagHash> is a mutable bag/multiset, meaning a collection of distinct
elements in no particular order that each have an integer weight assigned to
them signifying how many copies of that element are considered "in the bag".
(For I<immutable> bags, see L<Bag> instead.)

Objects/values of any type are allowed as bag elements.  Within a C<BagHash>,
items that would compare positively with the L<===> operator are considered the
same element, with the number of how many there were as its weight.  But of
course you can also easily get back the expanded list of items (without the
order):

=begin code
my $breakfast = <spam eggs spam spam bacon spam>.BagHash;

say $breakfast.elems;      # OUTPUT: «3␤»
say $breakfast.keys.sort;  # OUTPUT: «bacon eggs spam␤»

say $breakfast.total;      # OUTPUT: «6␤»
say $breakfast.kxxv.sort;  # OUTPUT: «bacon eggs spam spam spam spam␤»
=end code

C<BagHash>es can be treated as object hashes using the C<{ }> postcircumfix
operator, which returns the corresponding integer weight for keys that are
elements of the bag, and C<0> for keys that aren't.  It can also be used to
modify weights; setting a weight to C<0> automatically removes that element from
the bag, and setting a weight to a positive number adds that element if it
didn't already exist:

=begin code
my $breakfast = <spam eggs spam spam bacon spam>.BagHash;
say $breakfast<bacon>;     # OUTPUT: «1␤»
say $breakfast<spam>;      # OUTPUT: «4␤»
say $breakfast<sausage>;   # OUTPUT: «0␤»

$breakfast<sausage> = 2;
$breakfast<bacon>--;
say $breakfast.kxxv.sort;  # OUTPUT: «eggs sausage sausage spam spam spam spam␤»
=end code

=head1 Creating C<BagHash> objects

C<BagHash>es can be composed using C<BagHash.new>.  Any positional parameters,
regardless of their type, become elements of the bag:

    my $n = BagHash.new: "a", "b", "c", "c";
    say $n.perl;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
    say $n.keys.perl;        # OUTPUT: «("b", "a", "c").Seq␤»
    say $n.values.perl;      # OUTPUT: «(1, 1, 2).Seq␤»

Besides, C<BagHash.new-from-pairs> can create a C<BagHash> with items and their
occurrences.

    my $n = BagHash.new-from-pairs: "a" => 0, "b" => 1, "c" => 2, "c" => 2;
    say $n.perl;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»
    say $n.keys.perl;        # OUTPUT: «("b", "c").Seq␤»
    say $n.values.perl;      # OUTPUT: «(1, 4).Seq␤»

Alternatively, the C<.BagHash> coercer (or its functional form, C<BagHash()>)
can be called on an existing object to coerce it to a C<BagHash>.  Its semantics
depend on the type and contents of the object.  In general it evaluates the
object in list context and creates a bag with the resulting items as elements,
although for Hash-like objects or Pair items, only the keys become elements of
the bag, and the (cumulative) values become the associated integer weights:

    my $m = ("a", "b", "c", "c").BagHash;
    my $n = ("a" => 0, "b" => 1, "c" => 2, "c" => 2).BagHash;
    say $m.perl;             # OUTPUT: «("b"=>1,"a"=>1,"c"=>2).BagHash␤»
    say $n.perl;             # OUTPUT: «("b"=>1,"c"=>4).BagHash␤»

=head1 Operators

Perl 6 provides common set and bag operators, which can take C<BagHash>es (or
any other collections) as input, and return result as C<Bool>, C<Set> or C<Bag>
values. For example:

=begin code
my ($a, $b) = BagHash.new(2, 2, 4), BagHash.new(2, 3, 3, 4);

say $a (<) $b;   # OUTPUT: «False␤»
say $a (<+) $b;  # OUTPUT: «False␤»
say $a (^) $b;   # OUTPUT: «bag(3(2), 2)␤»
say $a (+) $b;   # OUTPUT: «bag(2(3), 4(2), 3(2))␤»

# Unicode versions:
say $a ⊂ $b;  # OUTPUT: «False␤»
say $a ≼ $b;  # OUTPUT: «False␤»
say $a ⊖ $b;  # OUTPUT: «bag(3(2), 2)␤»
say $a ⊎ $b;  # OUTPUT: «bag(2(3), 4(2), 3(2))␤»
=end code

See L<Set/Bag Operators|/language/setbagmix#Set/Bag_Operators> for a complete list of
set and bag operators with detailed explanations.

=head1 See Also

L<Sets, Bags, and Mixes|/language/setbagmix>

=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
